<!-- 保养分类 -->
<template>
  <div class="app-container">
    <div class="container">
      <div class="search-and-button">
        <el-input v-model="search.keyword" placeholder="请输入关键字进行搜索" clearable @keyup.enter.native="getTableInfo()" @clear="getTableInfo()">
          <el-button slot="append" icon="el-icon-search" @click="getTableInfo()" />
        </el-input>
        <div class="btnList">
          <el-button type="primary" icon="el-icon-plus" @click="showDialog(null)">新建</el-button>
          <ImportFile url="/userInfo/import" @getTableInfo="getTableInfo" />
          <el-button type icon="el-icon-refresh-left" @click="$router.push('/reload')">刷新</el-button>
          <el-button type icon="el-icon-more">更多</el-button>
        </div>
      </div>
      <el-row class="table">
        <el-table ref="tableData" :data="tableData" stripe border>
          <el-table-column type="selection" align="center"></el-table-column>
          <el-table-column align="center" type="index" label="索引" width="60px" />
          <el-table-column v-for="item in fieldList" :key="item.id" :align="item.align || 'left'" :prop="item.code.split(':')[1]" :label="item.val" :min-width="item.width + 'px'" show-overflow-tooltip>
            <template #default="{ row }">
              <span v-if="item.code.split(':')[1] === 'classifyCode'" class="tabNoStyle" @click="classifyCodeBtn(row)">{{ row.classifyCode}}</span>
              <span v-else-if="item.code.split(':')[1] === 'status'">{{ row.status == 1 ? '正常':'停用' }}</span>
              <span v-else>{{ row[item.code.split(':')[1]] }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" align="center" width="100px">
            <template #default="{ row }">
              <el-button-group>
                <el-button type="text" size="medium" @click="showDialog(row)">
                  <i class="iconfont icon-search"></i>
                </el-button>
              </el-button-group>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row>
        <div class="table-btn-box">
          <el-button @click="
            () => {
              tableData.forEach(row => {
                $refs['tableData'].toggleRowSelection(row, true)
                if (row.children && row.children.length > 0) {
                  row.children.forEach(child => {
                    $refs['tableData'].toggleRowSelection(child, true)
                  })
                }
              })
            }
          ">选择全部</el-button>
          <el-button @click="
            () => {
              tableData.forEach(row => {
                $refs['tableData'].toggleRowSelection(row)
                if (row.children && row.children.length > 0) {
                  row.children.forEach(child => {
                    $refs['tableData'].toggleRowSelection(child)
                  })
                }
              })
            }
          ">反向选择</el-button>
          <el-button>
            更多菜单
            <i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
        </div>
        <pagination ref="Pagination" :total="total" :current.sync="search.current" :size.sync="search.size" @pagination="getTableInfo" />
      </el-row>
      <!-- 新增编辑 -->
      <modify ref="addAndModify" @getTableInfo="getTableInfo" />
      <set-item ref="setItem" @getTableInfo="getTableInfo" />
    </div>
  </div>
</template>
<script>
import ImportFile from '@/components/ImportFile'
import modify from './components'
import setItem from './components/setItem'
import Pagination from '@/components/Pagination'
export default {
  components: { modify, Pagination, ImportFile, setItem },
  data() {
    return {
      search: {
        current: 1,
        size: 20,
        keyword: ''
      },
      tableData: [],
      total: 0
    }
  },
  computed: {},
  mounted() {
    this.getTableInfo()
  },
  methods: {
    async getTableInfo(page = 1) {
      this.search.current = page
      this.loading = true
      const res = await this.$axios.post(
        '/equip/maintain/classify/list',
        this.search
      )
      if (res?.data.code === 200) {
        this.tableData = res.data.data.list
        this.total = res.data.data.total
        setTimeout(() => {
          this.loading = false
        }, 100)
      } else {
        this.loading = false
      }
    },
    /**
     * @description: 新增编辑
     * @return {*}
     */
    async showDialog(row) {
      this.$refs['addAndModify'].showDialog(row)
    },
    classifyCodeBtn(row) {
      this.$refs['setItem'].showDialog(row)
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
